package com.best.smart.logfile;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;


public class LogAdapter extends RecyclerView.Adapter<LogAdapter.ConfigHolder> {


    private List<String> mList;
    private LayoutInflater mLayoutInflater;
    private FragmentActivity mContext;
    ;

    public LogAdapter(FragmentActivity context, List<String> list) {
        mLayoutInflater = LayoutInflater.from(context);
        this.mList = list;
        mContext = context;
    }

    @Override
    public int getItemCount() {
        if (mList == null)
            return 0;
        return mList.size();
    }

    @Override
    public ConfigHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mSelectListener = new SelectListener() {
            @Override
            public void select(String bean) {
                CustomDialog dialog = CustomDialog.newInstance( bean);
                dialog.show(mContext.getSupportFragmentManager(), "");



            }
        };
        return new ConfigHolder(mLayoutInflater.inflate(R.layout.item_log, parent, false), mContext, mSelectListener);
    }

    @Override
    public void onBindViewHolder(ConfigHolder holder, int position) {
        ((ConfigHolder) holder).onBind(mList.get(position));
    }

    public List<String> getList() {
        return mList;
    }

    class ConfigHolder extends RecyclerView.ViewHolder {



        private TextView tv_info;
        private SelectListener mSelectListener;
        private View rootView;
        private Context mContext;


        public ConfigHolder(View itemView, Context context, SelectListener mSelectListener) {
            super(itemView);
            mContext = context;
            tv_info = itemView.findViewById(R.id.tv_info);
            rootView = itemView.findViewById(R.id.root_view);
            this.mSelectListener = mSelectListener;
        }

        public void onBind(String bean) {
            tv_info.setText(bean);
            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mSelectListener != null){
                        mSelectListener.select(bean);
                    }
                }
            });
        }
    }

    SelectListener mSelectListener;

    public void setSelectListener(SelectListener listener) {
        mSelectListener = listener;
    }

    public interface SelectListener {
        void select(String bean);

    }


}


